﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class FrmCountSpec : System.Windows.Forms.Form
	{
		public FrmCountSpec()
		{
			this.InitializeComponent();
			base.ShowInTaskbar = false;
		}

		public FrmCountSpec(ExternalCommandData cmdData, string msg, ElementSet elements)
		{
			this.InitializeComponent();
			this.cmdDataForm = cmdData;
			this.msgForm = msg;
			this.elementsForm = elements;
			base.ShowInTaskbar = false;
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			WinFormTools.SetPanelColor(this.panel1, e);
		}

		private void BindData()
		{
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = false;
			DataGridViewImageColumn dataGridViewImageColumn = new DataGridViewImageColumn();
			dataGridViewImageColumn.Name = "Drop";
			dataGridViewImageColumn.HeaderText = "下拉";
			dataGridViewImageColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "Name";
			dataGridViewTextBoxColumn.HeaderText = "类型名称";
			dataGridViewTextBoxColumn.Width = 72;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn);
			DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();
			dataGridViewCheckBoxColumn.Name = "isCheck";
			dataGridViewCheckBoxColumn.HeaderText = "选";
			dataGridViewCheckBoxColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewCheckBoxColumn);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCountSpec);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("Elements/Spec");
			this.dataGridView1.Rows.Add(xmlNodeList.Count);
			for (int i = 0; i < xmlNodeList.Count; i++)
			{
				if (xmlNodeList.Item(i).Attributes["Check"].Value == "true")
				{
					this.dataGridView1.Rows[i].Cells[0].Value = true;
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[0].Value = false;
				}
				this.dataGridView1.Rows[i].Cells[1].Value = xmlNodeList.Item(i).Attributes["Name"].Value;
				if (i == 0)
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[2];
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[3];
				}
			}
			this.dataGridView1.Width = 113;
			this.dataGridView1.Height = xmlNodeList.Count * 23;
			this.panel1.Width = 120;
			this.panel1.Height = this.dataGridView1.Height + 2;
			base.Width = this.panel1.Width;
			base.Height = this.panel1.Height;
			this.textBox1.Focus();
		}

		private void FrmCountSpec_Load(object sender, EventArgs e)
		{
			this.BindData();
			this.textBox1.Focus();
		}

		private XmlDocument GetXmlDoc()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(XmlInfo.xmlCountSpec);
			return xmlDocument;
		}

		private void FrmParentMethod()
		{
			if (this.classSel != null)
			{
				this.classSel();
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				if (e.ColumnIndex == 2 && e.RowIndex == 0)
				{
					this.FrmParentMethod();
					base.Hide();
				}
				else
				{
					XmlDocument xmlDoc = this.GetXmlDoc();
					XmlNodeList xmlNodeList = xmlDoc.SelectNodes("Elements/Spec");
					if (e.ColumnIndex == 0)
					{
						if (xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value == "true")
						{
							xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value = "false";
						}
						else
						{
							xmlNodeList.Item(e.RowIndex).Attributes["Check"].Value = "true";
						}
					}
					XmlInfo.xmlCountSpec = xmlDoc.InnerXml;
				}
			}
			catch
			{
			}
		}

		private ExternalCommandData cmdDataForm;

		private string msgForm;

		private ElementSet elementsForm = new ElementSet();

		public FrmCountSpec.delegateSel classSel;

		public delegate void delegateSel();
	}
}
